import json
from datetime import datetime

def reformat_school_data():
    try:
        # 读取原始JSON数据
        with open('学校代码数据_原始.json', 'r', encoding='utf-8') as f:
            data = json.load(f)
        
        # 打开文件准备写入
        with open('学校代码数据_行格式.txt', 'w', encoding='utf-8') as f:
            # 写入文件头部信息
            f.write(f"获取时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n")
            f.write("==== 学校代码列表 ====\n\n")
            
            # 获取学校数据
            schools_data = data.get('data', {})
            
            # 遍历每个学校并写入一行
            for code, school_info in schools_data.items():
                school_id = school_info.get('school_id', '')
                name = school_info.get('name', '')
                f.write(f"代码: {code}, 学校ID: {school_id}, 学校名称: {name}\n")
            
        print(f"数据已重新格式化并保存到 学校代码数据_行格式.txt")
        
    except Exception as e:
        print(f"处理数据时出错: {str(e)}")

if __name__ == "__main__":
    reformat_school_data() 